package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionZHT

import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperEvaluationInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.PaperEvaluationInforIndexHelp.getChildListBean
import com.lancoo.znbkxx.teacher.widget.CustomTextViewToPicture
import kotlinx.android.synthetic.main.aibk_adapter_item_child_xzt.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_itemanalysis.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_ppt_question_index.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_table_view.view.*

/**
 * auther: 王国龙
 * Date：2020.05.22
 * describe:
 */
class EvaluationAdapterChildPpt(
        val activity: AppCompatActivity,
        val paperEvaluationInfor: PaperEvaluationInfor,
        val index_A: Int,
        val index_B: Int,
        val index_C: Int,
        val index_D:Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        var rootView:View? = null
        if(position == getChoiceTotalSize()+1){
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_itemanalysis, parent, false)
        }else if(position == 0){
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_ppt_question_index, parent, false)
        }else {
            rootView = LayoutInflater.from(activity)
                    .inflate(R.layout.aibk_adapter_item_child_xzt, parent, false)
        }
        return rootView
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        if(position == getChoiceTotalSize()+1){
            CustomTextViewToPicture(activity,holder.convertView.TvItemAnalysis, LancooMethod.getNewItemAnalysis(getHtmlAnalysisAll()))
        }else if(position == 0){
            val itemSortIndex = getChildListBean(paperEvaluationInfor,index_A,index_B,index_C).ItemList[index_D].ItemSortIndex
            holder.convertView.TvQuesIndex.text = "$itemSortIndex. "
        }else{
            var optionIndex = getChildListBean(paperEvaluationInfor,index_A,index_B,index_C).ChildOptionList[position-1].OptionIndex
            var option = getChildListBean(paperEvaluationInfor,index_A,index_B,index_C).ChildOptionList[position-1].Option
            val stuCount = getChildListBean(paperEvaluationInfor,index_A,index_B,index_C).ChildOptionList[position-1].StuCount
            val rate = getChildListBean(paperEvaluationInfor,index_A,index_B,index_C).ChildOptionList[position-1].Rate
            holder.convertView.BtnOptionIndex.text = optionIndex
            CustomTextViewToPicture(activity, holder.convertView.TvOptionContent, "$option($stuCount,$rate)")

            val itemAnswer = getChildListBean(paperEvaluationInfor,index_A,index_B,index_C).ItemList[index_D].ItemAnswer
            if (optionIndex == itemAnswer) {
                holder.convertView.BtnOptionIndex.setTextColor(activity.resources.getColor(R.color.lancoooWhite))
                holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_green)
                holder.convertView.TvOptionContent.setTextColor(activity.resources.getColor(R.color.lancoooGreen))
            } else {
                holder.convertView.BtnOptionIndex.setTextColor(activity.resources.getColor(R.color.textColor))
                holder.convertView.BtnOptionIndex.setBackgroundResource(R.mipmap.aibk_icon_choice_white)
                holder.convertView.TvOptionContent.setTextColor(activity.resources.getColor(R.color.textColor))
            }
        }

    }

    private fun getChoiceTotalSize():Int{
        return getChildListBean(paperEvaluationInfor,index_A,index_B,index_C).ChildOptionList.size
    }

    override fun getItemCount(): Int {
        return getChoiceTotalSize()+2
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    private fun getHtmlAnalysisAll(): String {

        var isHaveItemAnalysis = false
        var itemAnalysisAll = ""
        val childListSize = paperEvaluationInfor.TypeList[index_A].QuesDetailList[index_B].ChildList.size
        for (c in 0 until childListSize) {
            val sortIndxType = getChildListBean(paperEvaluationInfor,index_A,index_B,c).SortIndexType
            if (sortIndxType == 0) {
                var childAnalysis = getChildListBean(paperEvaluationInfor,index_A,index_B,c).ChildAnalysis
                if (childAnalysis.isEmpty()) {
                    childAnalysis = activity.getString(R.string.aibk_no_analyze) //...略
                } else {
                    isHaveItemAnalysis = true
                }
                if (itemAnalysisAll.isEmpty()) {
                    itemAnalysisAll = childAnalysis
                } else {
                    itemAnalysisAll = "$itemAnalysisAll<br>$childAnalysis"
                }
            } else if (sortIndxType == 1) {
                val itemListsize = getChildListBean(paperEvaluationInfor,index_A,index_B,c).ItemList.size
                for (b in 0 until itemListsize) {
                    val sortIndex = getChildListBean(paperEvaluationInfor,index_A,index_B,c).ItemList[b].SortIndex
                    var ItemAnalysis = getChildListBean(paperEvaluationInfor,index_A,index_B,c).ItemList[b].ItemAnalysis
                    if (ItemAnalysis.isEmpty()) {
                        ItemAnalysis = activity.getString(R.string.aibk_no_analyze) //...略
                    } else {
                        isHaveItemAnalysis = true
                    }
                    if (itemAnalysisAll.isEmpty()) {
                        itemAnalysisAll = "<b>$sortIndex. </b>$ItemAnalysis"
                    } else {
                        itemAnalysisAll = "$itemAnalysisAll<br><b>$sortIndex. </b>$ItemAnalysis"
                    }
                }
            }
            if (!isHaveItemAnalysis) {
                itemAnalysisAll = activity.getString(R.string.aibk_no_analyze)
            }
        }

        return itemAnalysisAll
    }

}